package com.lcm.fas.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UIManager;

import com.lcm.fas.controller.FileController;
import com.lcm.fas.model.FileInfo;

public class FileInternalFrame1 extends JInternalFrame implements ActionListener{
	private static final long serialVersionUID = 1L;
//	private static boolean darkMode =false;
	private static JTable table;
	private static List<String> copyOrMoveFileSrc;//源文件路径：用于文件的复制、粘贴
	private static MyTableModel<FileInfo> tableModel;//自定义的表格模板
	private static JTextField filePath;
	private static JTextField newFileTf;
	private static JCheckBox selectType;//文件类型选择
	private static JComboBox<?> typeCB;//文件选择下拉列表
	private static JTextField selectCount;
	private static int selectedCount=0;
	private static String copyOrCut="";//当前是复制还是剪切
	private static JPanel panel;
	private static JLabel filePathLb;
	private static JPanel panel_1;
	private static JLabel newFileLb;
	private static JButton newDirBtn;
	private static JButton newFileBtn;
	private static JButton backBtn;
	private static JButton renamesBtn;
	private static JButton pasteBtn;
	private static JButton flush;
	private static JLabel selectedLb;
	
	/**
	 * Create the frame
	 */
	public FileInternalFrame1() {
		setTitle("文件管理系统");
		setIconifiable(true);//支持窗口最小化显示
		setClosable(true);//支持窗口可关闭
//		s
		//获取屏幕尺寸
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		//设置尺寸大小
		setBounds(0,0,1190,640);
		
		panel = new JPanel();
		panel.setBorder(UIManager.getBorder("Button.border"));
		getContentPane().add(panel, BorderLayout.NORTH);
		
		filePathLb = new JLabel("当前位置：");
		filePath = new JTextField();
		filePath.setEditable(false);
		filePath.setColumns(10);
		filePath.setText(MainFrame.ROOT_PATH);
		
		panel_1 = new JPanel();
		
		newFileLb = new JLabel("新文件/目录名称：");
		newFileTf = new JTextField();
		newFileTf.setColumns(10);
		
		selectType = new JCheckBox("文件类型：");
		
		typeCB = new JComboBox();
		typeCB.setModel(new DefaultComboBoxModel(new String[] {".*",".txt", ".docx", ".pptx",".sql",".pdm",".md"}));
		typeCB.setSelectedIndex(0);
		
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(filePathLb, GroupLayout.PREFERRED_SIZE, 103, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(filePath, GroupLayout.PREFERRED_SIZE, 321, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(newFileLb)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(newFileTf)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(selectType)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(typeCB, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE))
						.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 1158, GroupLayout.PREFERRED_SIZE))
					.addContainerGap())
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(filePathLb, GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
						.addComponent(filePath, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
						.addComponent(newFileLb, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
						.addComponent(newFileTf, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
						.addComponent(typeCB, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(selectType))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE))
		);
		
		newDirBtn = new JButton("创建为新文件夹");
		newDirBtn.setBackground(new Color(204, 153, 51));
		newDirBtn.setIcon(new ImageIcon(FileInternalFrame1.class.getResource("/image/新建文件夹.png")));
		setFontStyleForBtn(newDirBtn);
		newDirBtn.addActionListener(this);
		
		newFileBtn = new JButton("创建为新文件");
		newFileBtn.setBackground(new Color(255, 153, 0));
		newFileBtn.setIcon(new ImageIcon(FileInternalFrame1.class.getResource("/image/新建文件.png")));
		setFontStyleForBtn(newFileBtn);
		newFileBtn.addActionListener(this);
		
		backBtn = new JButton("返回上级菜单");
		backBtn.setBackground(new Color(0, 153, 153));
		backBtn.setIcon(new ImageIcon(FileInternalFrame1.class.getResource("/image/返回上一页 (3).png")));
		setFontStyleForBtn(backBtn);
		backBtn.addActionListener(this);
		
		renamesBtn = new JButton("批量命名");
		renamesBtn.setBackground(new Color(0, 153, 255));
		renamesBtn.addActionListener(this);
		renamesBtn.setIcon(new ImageIcon(FileInternalFrame1.class.getResource("/image/批量别名 (1).png")));
		setFontStyleForBtn(renamesBtn);
		renamesBtn.addActionListener(this);
		
		pasteBtn = new JButton("粘贴到此处");
		pasteBtn.setBackground(new Color(0, 204, 204));
		pasteBtn.setIcon(new ImageIcon(FileInternalFrame1.class.getResource("/image/粘贴.png")));
		setFontStyleForBtn(pasteBtn);
		pasteBtn.addActionListener(this);
		
		flush = new JButton("刷 新");
		flush.setBackground(new Color(0, 204, 51));
		flush.setIcon(new ImageIcon(FileInternalFrame1.class.getResource("/image/刷新 (1).png")));
		setFontStyleForBtn(flush);
		flush.addActionListener(this);
		
		selectedLb = new JLabel("已选：");
		selectCount = new JTextField();
		selectCount.setText("0");
		selectCount.setFont(new Font(MainFrame.fontType, Font.BOLD, 18));
		selectCount.setEditable(false);
		selectCount.setColumns(10);
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGap(6)
					.addComponent(backBtn)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(renamesBtn)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(pasteBtn, GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(newDirBtn, GroupLayout.PREFERRED_SIZE, 212, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(newFileBtn, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(flush, GroupLayout.PREFERRED_SIZE, 115, GroupLayout.PREFERRED_SIZE)
					.addGap(20)
					.addComponent(selectedLb)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(selectCount, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE)
					.addGap(63))
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGap(0, 0, Short.MAX_VALUE)
					.addComponent(newDirBtn, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
					.addContainerGap())
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(newFileBtn, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
						.addComponent(selectedLb, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
						.addComponent(flush, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
						.addComponent(selectCount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(pasteBtn, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
						.addComponent(renamesBtn, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
						.addComponent(backBtn, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		panel_1.setLayout(gl_panel_1);
		panel.setLayout(gl_panel);
		
//##############TODO 右键菜单的定义：PopupMenu对象
//		首先，需要声明一个PopupMenu对像。
		JPopupMenu popupMenu1 = new JPopupMenu();
//		然后新建一些子菜单，这里使用建立6个菜单项
		JMenuItem menuItem1 = new JMenuItem("打开");
		JMenuItem menuItem2 = new JMenuItem("复制");
		JMenuItem menuItem3 = new JMenuItem("剪切");
		JMenuItem menuItem4 = new JMenuItem("粘贴");
		JMenuItem menuItem7 = new JMenuItem("删除");
		JMenuItem menuItem5 = new JMenuItem("重命名");
		
		menuItem1.setFont(new Font(MainFrame.fontType, Font.PLAIN, 20));
		menuItem2.setFont(new Font(MainFrame.fontType, Font.PLAIN, 20));
		menuItem3.setFont(new Font(MainFrame.fontType, Font.PLAIN, 20));
		menuItem4.setFont(new Font(MainFrame.fontType, Font.PLAIN, 20));
		menuItem5.setFont(new Font(MainFrame.fontType, Font.PLAIN, 20));
		menuItem7.setFont(new Font(MainFrame.fontType, Font.PLAIN, 20));
//		然后再出初化，并加入监听事件中：
		menuItem1.addActionListener(this);
		menuItem2.addActionListener(this);
		menuItem3.addActionListener(this);
		menuItem4.addActionListener(this);
		menuItem5.addActionListener(this);
		menuItem7.addActionListener(this);
		
		popupMenu1.add(menuItem1);
		popupMenu1.add(menuItem2);
		popupMenu1.add(menuItem3);
		popupMenu1.add(menuItem4);
		popupMenu1.add(menuItem7);
		popupMenu1.add(menuItem5);
		//在this中加入popupMenu1
		getContentPane().add(popupMenu1);
		
//#########TODO 表格实例化（Customer）
		//表格标题行
		String[] columnNames = {"文件名称","修改时间","文件类型","文件大小","文件路径"};
		tableModel = new MyTableModel<FileInfo>(columnNames, FileInfo.class);//实例化模板
		table = new JTable(tableModel);//创建表格面板
		//单击事件定义
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(e.getButton()==MouseEvent.BUTTON3) {//右键事件
					popupMenu1.show(table,e.getX(), e.getY());
				}else if (e.getButton()==MouseEvent.BUTTON1 && e.getClickCount()==2) {//双击事件
					if(table.getValueAt(table.getSelectedRow(), 2).equals("文件夹")) {//双击进入文件夹
						filePath.setText((String)table.getValueAt(table.getSelectedRow(), 4));//更新URL
						initTable(filePath.getText().trim());
					}else {//调用系统程序打开文件
						try {
							Runtime.getRuntime().exec("rundll32 url.dll FileProtocolHandler " + table.getValueAt(table.getSelectedRow(), 4));
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
							JOptionPane.showMessageDialog(null, "文件打开失败！可能是不允许的格式文件！");
						}
					}
				}
			}
		});
		
		table.setPreferredScrollableViewportSize(new Dimension(screenSize.width*2/3-60,screenSize.height/3));
		//视图高度填充
		table.setFillsViewportHeight(true);
		table.setAutoCreateRowSorter(true);
		table.setRowHeight(33);//设置行高
		//设置表头字体样式
		table.getTableHeader().setFont( new Font(MainFrame.fontType, Font.BOLD, 20) );
		table.getTableHeader().setBackground(new Color(250,217,127));
		//设置表内字体样式
		table.setFont(new Font(MainFrame.fontType, Font.PLAIN, 18));
		//将表格放入滚动面板中
		JScrollPane scrollPane = new JScrollPane(table);
		getContentPane().add(scrollPane, BorderLayout.CENTER);
		//初始化表格：
		initTable(MainFrame.ROOT_PATH);
//#######TODO  线程1： 实时更新选中的行数（每0.1秒刷新一次），实时记录选中的行索引
		new Thread(new Runnable() {
			@Override
			public void run() {
				while(true) {
//					try {
//						Thread.sleep(100);//间隔0.1秒获取一次选中的行数
						selectedCount = table.getSelectedRowCount();
						selectCount.setText(String.valueOf(selectedCount));
						setFontStyle(selectCount, 2);
						selectCount.setBackground(MainFrame.darkMode?MainFrame.bgColorAtDart:MainFrame.defaultBgColor);
//					}
//					catch (InterruptedException e) {
//						e.printStackTrace();
//					}
				}
			}
		}).start();
		
//		darkMode();//深色模式
//#######TODO  线程2： 深色模式的实现
		new Thread(new Runnable() {
			@Override
			public void run() {
				while(true) {
					darkMode();
					try {
						Thread.sleep(200);//间隔0.2秒刷新一次
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
	}
	
	/**
	 * 表格初始化
	 * @param path
	 */
	public static void initTable(String path) {
		List<FileInfo> files = FileController.getFiles(path);//根据根路径获取所有文件目录与文件
		//如果有内容则更新表格
		if(files!=null && files.size()>0) {//查询到有文件
			Vector<FileInfo> list = new Vector<FileInfo>();
			for (FileInfo f : files) {
				if(f.getType().equals("文件夹")) {//文件夹不用加大小
					f.setSize("");
				}
				list.add(f);
			}
			//更新数据
			tableModel.updateData(list);
			table.setRowSelectionInterval(0, 0);//默认选中第一行	
		}else {//没有文件
			if(copyOrMoveFileSrc!=null && copyOrMoveFileSrc.size()>0) {
				int choice1 = JOptionPane.showConfirmDialog(null, "当前目录下没有文件，是否要粘贴已复制/剪切的文件？","温馨提示",JOptionPane.YES_NO_OPTION);
				if(choice1==0) {
					pasteFile();
					return;
				}
			}
			int choice = JOptionPane.showConfirmDialog(null, "当前目录下没有文件，是否要创建文件？","温馨提示",JOptionPane.YES_NO_OPTION);
			if(choice==0) {
				String newFile = JOptionPane.showInputDialog(null,"请输入文件名及其后缀名(示例：text1.txt)：\n注：文件名为空则默认取消此操作！","新建文件.txt");
				if(newFile!=null && newFile.trim().length()>0) {
					if(newFile.contains(".") & newFile.lastIndexOf(".")<newFile.length()-1 & newFile.lastIndexOf(".")!=0) {
						boolean ok3 = FileController.addFileOrDir(filePath.getText()+"\\"+newFile, "file");
						if(ok3) {//创建成功
							JOptionPane.showMessageDialog(null, "新文件 "+newFile+" 已创建成功！");
							initTable(filePath.getText());
						}else {
							JOptionPane.showMessageDialog(null, "新文件 "+newFile+" 创建失败！");
						}
					}else {
						JOptionPane.showMessageDialog(null, "新文件的文件类型未正确填写，创建失败！");
					}
				}
			}else {
				filePath.setText(filePath.getText().substring(0,filePath.getText().lastIndexOf("\\")));
			}
		}
	}
	
	//获取该目录下的所有文件名
	public static List<String> getAllFileAt(){
		int rowCount = table.getRowCount();
		List<String> list = null;
		if(rowCount>0) {
			list = new ArrayList<String>();
			for(int i=0;i<rowCount;i++) {
				list.add((String)table.getValueAt(i, 0));
			}
		}
		return list;
	}

	/**
	 * 点击事件的管理
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String command = e.getActionCommand();//获取点击事件源
		String selected = selectCount.getText();
		switch (command) {
			case "返回上级菜单":
				if(filePath.getText().equals(MainFrame.ROOT_PATH.subSequence(0, MainFrame.ROOT_PATH.lastIndexOf("\\")))) {
					JOptionPane.showMessageDialog(null, "当前目录已是磁盘根目录，不能再返回上一级了哦！");
				}else {
					String regionPath = filePath.getText();
					filePath.setText(regionPath.substring(0,regionPath.lastIndexOf("\\")));
					initTable(filePath.getText());
				}
				break;
			case "粘贴到此处":
				pasteFile();break;
			case "批量命名":{
				int[] selectedRows2 = table.getSelectedRows();
				if(selectedRows2.length==0) {//未选择
					JOptionPane.showMessageDialog(null, "请选择要进行批量命名的文件/文件夹！");
				}else {
				    int test1 = 0;
					System.out.println("次数："+test1++);
					String newName = JOptionPane.showInputDialog("请输入批量命名前缀（不用加后缀名）：");
					if(newName!=null && newName.length()>0) {
						int count = 0;//完成个数
						for (int i = 0; i < selectedRows2.length; i++) {
							String rePath = (String)table.getValueAt(i, 4);//原文件路径
							String newName2 = newName+(i+1);//文件夹不用加后缀名
							if(rePath.substring(rePath.lastIndexOf("\\")).contains(".")) {//命名对象是文件
								newName2 = newName+(i+1)+rePath.substring(rePath.lastIndexOf("."));
							}
							System.out.println("reName："+newName+"--》"+newName2);
							boolean r = FileController.reName(rePath, newName2);
							if(r) {
								count++;
							}else {
								continue;
							}
						}
						JOptionPane.showMessageDialog(null, "文件批量命名完成！\n批量任务数： "+selectedRows2.length+"\n"+"完成数： "+count);
						initTable(filePath.getText());//更新表格
					}
					System.out.println("我好了！");
				}
			}break;
			case "刷 新":
				initTable(filePath.getText().trim());//刷新表格
				break;
			case "创建为新文件夹":
				String newFile = newFileTf.getText().trim();
				if(newFile!=null && newFile.length()>0) {
					boolean ok = FileController.addFileOrDir(filePath.getText()+"\\"+newFile, "dir");
					if(ok) {
						JOptionPane.showMessageDialog(null, "文件夹  "+newFile+" 创建成功！");
						newFileTf.setText("");
						initTable(filePath.getText());
					}else {
						JOptionPane.showMessageDialog(null, "文件夹  "+newFile+" 创建失败！"+"\n请确认该目录下是否有含同名文件夹");
					}
				}else {
					JOptionPane.showMessageDialog(null, "请先输入要创建的文件夹名称！！");
				}
				break;
			case "创建为新文件":
				String newFile2 = newFileTf.getText().trim();
				if(newFile2!=null && newFile2.length()>0) {
					if(selectType.isSelected()) {
						String type = typeCB.getSelectedItem().toString();//获取选中的文件类型
						boolean ok2 = false;
						if(type.equals(".*")) {//如果是.*，需要输入后缀名
							if(newFile2.contains(".") && newFile2.lastIndexOf(".")<newFile2.length()-1) {
								ok2 =FileController.addFileOrDir(filePath.getText()+"\\"+newFile2, "file");
							}else {
								JOptionPane.showMessageDialog(null, "请在文件名后面添加自定义的后缀名！");
								return;
							}
						}else {
							ok2 = FileController.addFileOrDir(filePath.getText()+"\\"+newFile2+type, "file");
						}
						if(ok2) {
							JOptionPane.showMessageDialog(null, "文件   "+newFile2+" 创建成功！");
							initTable(filePath.getText());
							newFileTf.setText("");
						}else {
							JOptionPane.showMessageDialog(null, "文件   "+newFile2+" 创建失败！"+"\\n请确认该目录下是否有含同名文件");
						}
					}else {
						JOptionPane.showMessageDialog(null, "请勾选文件类型单选框，并选择文件类型！");
					}
				}else {
					JOptionPane.showMessageDialog(null, "请先输入要创建的文件名称！！");
				}
				break;
			case "打开":{
				try {
					if(Integer.parseInt(selected)==0) {//没有选中的文件
						JOptionPane.showMessageDialog(null, "请先选择要打开的文件");
					}else {
						if(table.getValueAt(table.getSelectedRow(), 2).equals("文件夹")) {//文件夹打开
							filePath.setText((String)table.getValueAt(table.getSelectedRow(), 4));
							initTable(filePath.getText());
						}else {//文件就用系统进程打开
							Runtime.getRuntime().exec("rundll32 url.dll FileProtocolHandler " + table.getValueAt(table.getSelectedRow(), 4));
						}
					}
				} catch (IOException e1) {
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "文件 "+table.getValueAt(table.getSelectedRow(), 0)+"打开失败");
				}
			}break;
			case "复制":
				if(Integer.parseInt(selectCount.getText().trim())>0) {//有选中的文件
					copyOrMoveFileSrc = new ArrayList<String>();//初始化
					int[] selectRow_2 = table.getSelectedRows();
					for(int i=0;i<Integer.parseInt(selectCount.getText());i++) {
						copyOrMoveFileSrc.add(table.getValueAt(selectRow_2[i], 4).toString());//保存被复制文件的路径
					}
					copyOrCut = "copy";
					JOptionPane.showMessageDialog(null, "文件已复制，快去粘贴吧！！");
				}else {
					JOptionPane.showMessageDialog(null, "请先选中要复制的文件");
				}
				break;
			case "剪切":
				if(Integer.parseInt(selectCount.getText())>0) {//有选中的文件
					copyOrMoveFileSrc = new ArrayList<String>();//初始化
					int[] selectRow_2 = table.getSelectedRows();
					for(int i=0;i<Integer.parseInt(selectCount.getText());i++) {
						copyOrMoveFileSrc.add(table.getValueAt(selectRow_2[i], 4).toString());//保存移动文件的源路径
					}
					copyOrCut = "cut";
					JOptionPane.showMessageDialog(null, "文件已剪切，快去粘贴吧");
				}else {
					JOptionPane.showMessageDialog(null, "请先选中要剪切的文件");
				}
				break;
			case "粘贴":pasteFile();break;
			case "重命名":{
				int rows = table.getSelectedRowCount();
				if(rows>1) {//选中多行
					JOptionPane.showMessageDialog(null, "一次只能重命名一个文件/文件夹哦");
				}else {
					String rePath = (String)table.getValueAt(table.getSelectedRow(), 4);//原文件路径
					String newName = JOptionPane.showInputDialog("请输入新的文件名（不用加后缀名）：\n注意：输入空内容相当于取消重名名\n");
					if(newName!=null && newName.trim().length()>0) {
						if(!table.getValueAt(table.getSelectedRow(), 2).equals("文件夹")) {//如果是文件，则补上后缀名
							newName += rePath.substring(rePath.lastIndexOf("."),rePath.length());
						}
						boolean r = FileController.reName(rePath, newName);
						if(r) {
							JOptionPane.showMessageDialog(null, "文件重命名成功！");
							initTable(filePath.getText().trim());
						}else {
							JOptionPane.showMessageDialog(null, "同一目录下已含有同名文件，重名名失败！");
						}
					}
				}
			}break;
			case "删除":{
				if(selectedCount==0) {//没有选中
					JOptionPane.showMessageDialog(null, "请先选中要删除的内容！");
				}else {
					//获取选中的行
					int[] selectedRows = table.getSelectedRows();
					int toDel = JOptionPane.showConfirmDialog(null, "确定要删除  "+table.getValueAt(selectedRows[0], 0)+"...等"+selectedCount+"个文件吗？","温馨提示",JOptionPane.YES_NO_OPTION);
					if(toDel==0) {//确认删除
						boolean deleted = false;
						for (int i : selectedRows) {
							String delPath = (String) tableModel.getValueAt(i, 4);//选中path列，删除文件依据的是文件URI
							deleted = FileController.deleteFile(delPath);
						}
						if(deleted) {//删除成功
							JOptionPane.showMessageDialog(null, "删除成功！");
							initTable(filePath.getText());
						}else {
							JOptionPane.showMessageDialog(null, "删除出错！");
						}
					}
				}
			}break;
			default:
				break;
		}
	}
	
	/**
	 * 设置字体样式
	 * @param component
	 */
	public static void setFontStyle(JComponent component,int fontClass) {
		switch (fontClass) {
		case 1://一级菜单
			component.setFont(new Font(MainFrame.fontType, Font.BOLD, 20));
			break;
		case 2://二级菜单
			component.setFont(new Font(MainFrame.fontType, Font.BOLD, 18));
			break;
		default: 
			break;
		}
		component.setForeground(MainFrame.darkMode?new Color(255, 255, 255):new Color(0, 0, 0));//根据深色模式来调整字体颜色
	}
	
	//深色模式
	public static void darkMode() {
		//深色模式下的字体样式调整
		setFontStyle(filePathLb, 2);
		setFontStyle(newFileTf, 2);
		setFontStyle(newFileLb, 2);
		setFontStyle(filePath, 2);
		setFontStyle(selectType, 2);
		setFontStyle(selectedLb, 2);
		setFontStyle(selectType, 2);
		setFontStyle(typeCB, 1);
		table.setFont(new Font(MainFrame.fontType,Font.PLAIN,20));
		table.setForeground(MainFrame.darkMode?new Color(255, 255, 255):new Color(0, 0, 0));
		setFontStyle(table.getTableHeader(), 1);
		//深色模式下背景颜色调整
		panel.setBackground(MainFrame.darkMode?MainFrame.bgColorAtDart:MainFrame.defaultBgColor);
		panel_1.setBackground(MainFrame.darkMode?MainFrame.bgColorAtDart:MainFrame.defaultBgColor);
		newDirBtn.setBackground(MainFrame.darkMode?new Color(180,134,44):new Color(204, 153, 51));
		newFileBtn.setBackground(MainFrame.darkMode?new Color(227,134,3):new Color(255, 153, 0));
		backBtn.setBackground(MainFrame.darkMode?new Color(5,137,137):new Color(0, 153, 153));
		renamesBtn.setBackground(MainFrame.darkMode?new Color(5,134,224):new Color(0, 153, 255));
		pasteBtn.setBackground(MainFrame.darkMode?new Color(3,175,175):new Color(0,204,204));
		flush.setBackground(MainFrame.darkMode?new Color(5,178,49):new Color(0, 204, 51));
		newFileTf.setBackground(MainFrame.darkMode?MainFrame.bgColorAtDart:MainFrame.defaultBgColor);
		filePath.setBackground(MainFrame.darkMode?MainFrame.bgColorAtDart:MainFrame.defaultBgColor);
		selectType.setBackground(MainFrame.darkMode?MainFrame.bgColorAtDart:MainFrame.defaultBgColor);
		typeCB.setBackground(MainFrame.darkMode?MainFrame.bgColorAtDart:MainFrame.defaultBgColor);
		table.setBackground(MainFrame.darkMode?MainFrame.bgColorAtDart:MainFrame.defaultBgColor);
		table.getTableHeader().setBackground(MainFrame.darkMode?new Color(227,196,116):new Color(250,217,127));
	}
	
	/**
	 * 给button组件文字设定样式
	 */
	public static void setFontStyleForBtn(JComponent component){
		component.setFont(new Font(MainFrame.fontType, Font.BOLD, 20));
		component.setForeground(new Color(255,255,255));
	}
	
	//文件粘贴
	public static void pasteFile() {
		//获取选中的行
		if(copyOrMoveFileSrc!=null && copyOrMoveFileSrc.size()>0) {//已经复制/剪切了文件了
			List<String> pasteFiles = copyOrMoveFileSrc;
			int toPaste = JOptionPane.showConfirmDialog(null, "确定要将  "+pasteFiles.get(0).substring(pasteFiles.get(0).lastIndexOf("\\"))+"...等"+pasteFiles.size()+"个文件粘贴到此处吗？","温馨提示",JOptionPane.YES_NO_OPTION);
			if(toPaste==0) {
				if(copyOrCut.equals("copy")) {//文件复制之后的粘贴
					List<String> errorFiles = new ArrayList<String>();
					for(int i=0;i<pasteFiles.size();i++) {
						String srcPath = pasteFiles.get(i);
						boolean ok = FileController.copy(srcPath, filePath.getText());
						if(!ok) {//粘贴失败！
							errorFiles.add(srcPath.substring(srcPath.lastIndexOf("\\")+1));
						}
					}
					if(errorFiles.size()>0) {
						String fails = "";
						for(int j=0;j<errorFiles.size();j++) {
							fails += (j+1)+"  "+errorFiles.get(j)+"\n";
						}
						JOptionPane.showMessageDialog(null, "下列文件/文件夹复制->粘贴失败\n"+fails+"请检查是否含有同名文件，或者目标文件是否存在!");
					}else {//移到成功！还原剪切板
						copyOrMoveFileSrc.removeAll(pasteFiles);//清空
						copyOrCut = "";
						initTable(filePath.getText());
					}
				}else if(copyOrCut.equals("cut")){//文件移动（剪切）的粘贴
					List<String> errorFiles = new ArrayList<String>();
					for(int i=0;i<pasteFiles.size();i++) {
						String srcPath = pasteFiles.get(i);
						boolean ok = FileController.moveFile(srcPath, filePath.getText()+srcPath.substring(srcPath.lastIndexOf("\\")));
						if(!ok) {//移动失败！
							errorFiles.add(srcPath.substring(srcPath.lastIndexOf("\\")+1));
						}
					}
					if(errorFiles.size()>0) {
						String fails = "";
						for(int j=0;j<errorFiles.size();j++) {
							fails += (j+1)+"  "+errorFiles.get(j)+"\n";
						}
						JOptionPane.showMessageDialog(null, "下列文件/文件夹剪切->粘贴失败\n"+fails+"请检查是否含有同名文件，或者目标文件是否存在!");
					}else {//移到成功！还原剪切板
						copyOrMoveFileSrc.removeAll(pasteFiles);//清空
						copyOrCut = "";
						initTable(filePath.getText());
					}
				}
			}
		}else {
			JOptionPane.showMessageDialog(null, "你还没复制/剪切文件哦，无文件可粘贴！");
		}
	}
	
	
}
